<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>Bootloader Development</title>
		<link rel="stylesheet" type="text/css" href="./images/style.css">
	</head>
	
	<body>
		<header class="banner">
			<h1>PIC16系列MCU 串口自举程序（Bootloader）开发</h1>
		</header>
		
		<main>
			<section>
				<h2>1. 简介</h2>
				<article>
					<p>本示例为基于MCC环境快速开发的PIC16系列MCU(PIC16F15376)的串口自举程序。</p>
				</article>
			</section>
			
			<section>
				<h2>2. 工具</h2>
				<article>
					<header>
					<h3>2.1. 软件开发环境</h3>
					</header>
					<p>相关软件开发版本情况说明如下。</p>
					<ul>
					<li><a href="https://www.microchip.com/mplabx" target="_blank">Microchip MPLAB® xIDE version 5.50 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/mplab-xc-compilers" target="_blank">Microchip MPLAB® XC8 C compiler version 2.32 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip MPLAB® Code Configurator (MCC) version 4.2.4 (with core v5.2.4) or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip MPLAB® Code Configurator (MCC) Device Libraries PIC10 / PIC12 / PIC16 / PIC18 MCUs version 1.81.7 or newer</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tools-tools-and-software/embedded-software-center/mplab-code-configurator" target="_blank">Microchip 18-bit Bootloader version 2.4.1 or newer</a></li>
					<li><a href="https://packs.download.microchip.com/" target="_blank">Microchip PIC16F1xxxx Series Device Support version 1.8.149 or newer</a></li>
					</ul>
				</article>
				<article>
					<header>
					<h3>2.2. 硬件开发环境</h3>
					</header>
					<p>该例程基于如下硬件开发。</p>
					<ul>
					<li><a href="https://www.microchip.com/en-us/development-tool/DM164148" target="_blank">PIC16F15376 CURIOSITY NANO EVALUATION KIT (Part Number: DM164148)</a></li>
					<img src="./images/3028-dm164148-crop.jpg" width="20%"; style="margin-left: 40px;">
					</ul>
				</article>
				<article>
					<header>
					<h3>2.3. 参考资料</h3>
					</header>
					<p>相关参考资料如下。</p>
					<ul>
					<li><a href="https://www.microchip.com/en-us/product/PIC16F15376" target="_blank">PIC16F15376 Data Sheet</a></li>
					<li><a href="https://www.microchip.com/en-us/development-tool/DM164148" target="_blank">Curiosity Low Pin Count Development Board User's Guide</a></li>
					</ul>
				</article>
			</section>
			
			<section>
				<h2>3. 自举程序工程创建(仅对关键部分说明)</h2>
				<article>
					<header>
						<h3>3.1. 打开MCC，在Sytem Module下对系统时钟进行如下设置。</h3>
					</header>
					<img src="./images/boot_setup_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.2. 添加Bootloader Generator库、EUSART2和MEMORY外设，并对Bootloader Generator进行如下设置。</h3>
					</header>
					<img src="./images/boot_setup_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.3. EUSART2采用默认配置。</h3>
					</header>
					<img src="./images/boot_setup_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.4. 按下图对相关管脚进行设置，其中RD0是串口发送引脚、RD1是串口接收引脚；RE2是CNANO板的按键。</h3>
					</header>
					<img src="./images/boot_setup_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.5. 设置自举程序工程代码空间范围。</h3>
					</header>
					<img src="./images/boot_setup_5.png" width="80%">
				</article>
				<article>
					<header>
						<h3>3.6. 点击下图编译按钮，自举程序工程可成功编译。到此，自举程序工程开发完毕。</h3>
					</header>
					<img src="./images/boot_setup_6.png" width="80%">
				</article>
			</section>
			
			<section>
				<h2>4. 应用程序工程创建(仅对关键部分说明)</h2>
				<article>
					<header>
						<h3>4.1. 打开MCC，在Sytem Module下对系统时钟进行如下设置，保证与自举工程程序一致。</h3>
					</header>
					<img src="./images/app_setup_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.2. 添加外设资源TMR0到项目资源列表中，并按如下设置。该TMR0用于实现CANNO板的LED 500ms闪烁。</h3>
					</header>
					<img src="./images/app_setup_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.3. 按下图对相关管脚进行设置，其中RE0是CNANO板的LED。</h3>
					</header>
					<img src="./images/app_setup_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.4. 对main.c文件做如下更改，其中右侧为更改后文件内容。使能中断，TMR0_UserInterruptHandler()函数用于CNANO板的LED 500ms闪烁。</h3>
					</header>
					<img src="./images/app_setup_4.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.5. 设置应用程序工程代码的地址偏移，此偏移根据自举程序工程代码空间范围而定。</h3>
					</header>
					<img src="./images/app_setup_5.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.6. 设置应用程序工程代码的地址范围，防止分配到checksum校验码所占用的最后2个字。</h3>
					</header>
					<img src="./images/app_setup_6.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.7. 应用程序工程编译后执行hexmate语句"hexmate ${ImagePath} -FILL=0x3FFF@0x0000:0x7FFB -CK=0x0800-0x7FFB@0x7FFC+0x0w-2t34g2 -o${ImagePath}"，该语句用于生成应用程序代码的和校验，校验规则与自举程序工程相匹配。</h3>
					</header>
					<img src="./images/app_setup_7.png" width="80%">
				</article>
				<article>
					<header>
						<h3>4.8. 对工程进行编译，以用于后续实验验证的boot和app hex文件合并。</h3>
					</header>
				</article>
			</section>
			
			<section>
				<h2>5. 串口升级实验验证</h2>
				<article>
					<header>
						<h3>5.1. 运行scripts文件夹下的hex_merge_app_boot.bat，将boot和app工程的hex文件合并。</h3>
					</header>
					<img src="./images/verify_1.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.2. 用usb线缆连接CNANO开发板，运行“MPLAB X IPE”烧录合并后的hex程序。</h3>
					</header>
					<img src="./images/verify_2.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.3. 确定CNANO开发板的串口号，接着对scripts文件夹下的serial_upload.bat进行相应修改，之后点击该批处理文件完成升级工作。升级成功后，CNANO开发板的LED以500ms闪烁。</h3>
					</header>
					<img src="./images/verify_3.png" width="80%">
				</article>
				<article>
					<header>
						<h3>5.4. 若再次升级，需要拔出CNANO开发板的USB线缆，按住开发板按键后在插入usb线缆，接着释放按键，既可以再次重复5.3步骤。</h3>
					</header>
				</article>
			</section>
		</main>
	</body>
</html>